package com.ayf.payment.game.api.exception;

import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CommonResult;
import com.ayf.payment.game.api.utils.SnowFlakeUtils;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 *
 *
 * @author wwl
 * @date 2019/12/5 14:36
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理 Exception 异常
     *
     * @param httpServletRequest httpServletRequest
     * @param e                  异常
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public CommonResult<?> exceptionHandler(HttpServletRequest httpServletRequest, Exception e) {
        String traceId = SnowFlakeUtils.getTraceId();
        LogPortal.error("{} 服务错误，发生 {} 异常，异常信息为:{}",e,traceId, e.getClass().getSimpleName(), e.getMessage());
        return new CommonResult<>(CommonConstants.RETURN_ERROR, "错误码：" + traceId);
    }

    @ResponseBody
    @ExceptionHandler(value = WxException.class)
    public CommonResult<?> wxExceptionHandler(WxException e) {
        return new CommonResult<>(e.getCode(), e.getMsg());
    }

    /**
     * 处理 BusinessException 异常
     *
     * @param httpServletRequest httpServletRequest
     * @param e                  异常
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public CommonResult businessExceptionHandler(HttpServletRequest httpServletRequest, BusinessException e) {
        LogPortal.error("业务异常。code:{}  msg:{}",e,e.getCode(), e.getMessage());
        return new CommonResult(CommonConstants.RETURN_ERROR, e.getMessage(),"");
    }

    /**
     * 处理 DataIntegrityViolationException 异常
     *
     * @param httpServletRequest httpServletRequest
     * @param e                  异常
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = DataIntegrityViolationException.class)
    public CommonResult jdbcExceptionHandler(HttpServletRequest httpServletRequest, DataIntegrityViolationException e) {
        return new CommonResult(CommonConstants.RETURN_ERROR, "数据操作异常","");
    }

    /**
     * 处理 NullSetException 异常
     *
     * @param e             异常
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = NullSetException.class)
    public CommonResult NullSetExceptionHandler(NullSetException e) {
        LogPortal.error("空结果集异常 >>> code:[{}] msg:[{}]",e.getCode(),e.getMsg());
        return new CommonResult(CommonConstants.RETURN_ERROR, e.getMsg(),null);
    }

    @ResponseBody
    @ExceptionHandler(value = ParameterListException.class)
    public Map<String, Object> ParameterListExceptionHandler(ParameterListException e) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", e.getCode());
        map.put("msgList", e.getMsgList());
        return map;
    }

    /**
     * 处理 ParameterException 异常
     *
     * @param e 异常
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = ParameterException.class)
    public CommonResult ParameterExceptionHandler(ParameterException e) {
        LogPortal.error("参数异常 >>> code:[{}] msg:[{}]",e.getCode(),e.getMsg());
        return new CommonResult(CommonConstants.RETURN_ERROR, e.getMsg(),null);
    }

    /**
     * 处理 AuthorizeFailedException 异常
     *
     * @param e 异常
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = AuthorizeFailedException.class)
    public CommonResult AuthorizeFailedExceptionHandler(AuthorizeFailedException e) {
        return new CommonResult(e.getCode(), e.getMsg(),null);
    }

    /**
     * 处理 AuditedFailedException 异常
     *
     * @param e 异常
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = AuditedFailedException.class)
    public CommonResult AuditedFailedExceptionHandler(AuditedFailedException e) {
        return new CommonResult(e.getCode(), e.getMsg());
    }

    /**
     * 处理 LogicalException 异常
     *
     * @param e 异常
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = LogicalException.class)
    public CommonResult LogicalExceptionHandler(LogicalException e) {
        LogPortal.error("逻辑异常:" + e.getMsg());
        return new CommonResult(CommonConstants.RETURN_ERROR, "错误码[" + e.getTraceId() + "]");
    }
}
